﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RentHouse.DTO;
using RentHouse.IService;
using RentHouse.Common;
using RentHouse.Service.Entities;
using RentHouse.Service;

namespace RentHouse.Admin.Controllers
{
    public class AdminUserController : Controller
    {
        private readonly IAdminUserService _adminUserService;
        private readonly ICityService _cityService;
        private readonly IRolesService _rolesService;
        public AdminUserController(IAdminUserService adminUserService,ICityService cityService,IRolesService rolesService) {
            this._adminUserService = adminUserService;
            this._cityService = cityService;
            this._rolesService = rolesService;
   
        }
        // GET: AdminUser
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult GetList(int length, int start, string name)
        {
            int count = 0;
            List<AdminUserDTO> list = _adminUserService.GetList(length, start, name, ref count);
            var result = new { data = list, recordsTotal = count, recordsFiltered = count };
            return Json(result, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Add()
        {
            var list = _rolesService.GetList();
            ViewBag.CityList = _cityService.GetCity().Select(e=> new SelectListItem{ 
            Text=e.Name,
            Value=e.Id.ToString()
            });
            return View(list);
        }
        [HttpPost]
        public ActionResult Add(AdminUserAddDTO adminUserAddDTO)
        {
            if (!ModelState.IsValid)
            { 
                return Json(new AjaxResult(ResultState.Error, MVCHelper.GetValidMsg(ModelState)));

            }
            else
            {
                var result = _adminUserService.Add(adminUserAddDTO);
                return Json(result);
            }
            
        }

        public ActionResult Delete(long id)
        {
            var result = _adminUserService.Delete(id);
            return Json(result);
        }
        public ActionResult Deletes(long[] id)
        {
            var result = _adminUserService.Deletes(id);
            return Json(result);
        }

    }
}